import subprocess

from .exceptions import ExecutionError
from .node import Node

class CommandNode(Node):
    def __init__(self,
                 command,
                 *args,
                 **kwargs):
        super(CommandNode, self).__init__(*args, **kwargs)

        self.command = command

    @property
    def full_command(self):
        return self.command.format(
            source=self.source,
            target=self.target)

    def execute(self):
        # TODO: Capture output can print it.

        rslt = subprocess.call(
            self.full_command.split())

        if rslt != 0:
            raise ExecutionError('Error executing command. (result={})'.format(rslt))

    @property
    def signature(self):
        return self.full_command

    @property
    def id(self):
        return self.full_command
